<template xlang="wxml">
  <view>
    <block v-for="(item, index) in treeList" :key="index">
      <view
        class="tki-tree-item"
        :style="[{
          paddingLeft: item.rank*15 + 'px',
          zIndex: item.rank*-1 +50
        }]"
        :class="{
          border: border === true,
          show: item.show,
          last: item.lastRank,
          showchild: item.showchild,
          open: item.open,
        }">
        <view class="tki-tree-label">
          <image
            class="tki-tree-icon" :src="item.lastRank ? lastIcon : item.showChild ? currentIcon : defaultIcon"
            @click.stop="showChildren(item)"/>
          <view style="width: 100%">
            <slot :item="item">
              {{ item.name }}
            </slot>
          </view>
        </view>
      </view>
    </block>
  </view>
</template>

<script>
export default {
  name: 'TreeList',
  props: {
    list: {
      type: Array,
      default: function () {
        return []
      }
    },
    idKey: {
      type: String,
      default: 'id'
    },
    rangeKey: {
      type: String,
      default: 'name'
    },
    foldAll: { // 折叠时关闭所有已经打开的子集，再次打开时需要一级一级打开
      type: Boolean,
      default: false
    },
    currentIcon: { // 展开时候的ic
      type: String,
      default: ''
    },
    defaultIcon: { // 折叠时候的ic
      type: String,
      default: ''
    },
    lastIcon: { // 没有子集的ic
      type: String,
      default: ''
    },
    border: { // 是否有分割线
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeList: [],
      selectIndex: -1,
      idArr: []
    }
  },
  computed: {},
  watch: {
    list: {
      deep: true, // 深度监听
      immediate: true, // 初次监听即执行
      handler(val) {
        this.initTree(val)
      }
    }
  },
  methods: {
    showChildren(item, index) {
      const list = this.treeList
      const id = item.id
      item.showChild = !item.showChild
      item.open = item.showChild ? true : !item.open
      list.forEach((childItem, i) => {
        if (item.showChild === false) {
          // 隐藏所有子级
          if (!childItem.parentId.includes(id)) {
            return
          }
          if (!this.foldAll) {
            if (childItem.lastRank !== true && !childItem.open) {
              childItem.showChild = false
            }
            // 为隐藏的内容添加一个标记
            if (childItem.show) {
              childItem.hideArr[item.rank] = id
            }
          } else {
            if (childItem.lastRank !== true) {
              childItem.showChild = false
            }
          }
          childItem.show = false
        } else {
          // 打开子集
          if (childItem.parentId[childItem.parentId.length - 1] === id) {
            childItem.show = true
          }
          // 打开被隐藏的子集
          if (childItem.parentId.includes(id) && !this.foldAll) {
            // console.log(childItem.hideArr)
            if (childItem.hideArr[item.rank] === id) {
              childItem.show = true
              if (childItem.open && childItem.showChild) {
                childItem.showChild = true
              } else {
                childItem.showChild = false
              }
              childItem.hideArr[item.rank] = null
            }
          }
        }
      })
    },
    renderTreeList(list = [], rank = 0, parentId = [], parents = []) {
      console.log('list', list)
      list.forEach(item => {
        this.treeList.push({
          id: item[this.idKey],
          name: item[this.rangeKey],
          source: item,
          parentId, // 父级id数组
          parents, // 父级id数组
          rank, // 层级
          showChild: true, // 子级是否显示
          open: true, // 是否打开
          show: true, // 自身是否显示
          hideArr: [],
          orChecked: item.checked ? item.checked : false,
          checked: item.checked ? item.checked : false
        })
        if (Array.isArray(item.children) && item.children.length > 0) {
          // console.log(item)
          var parentid = [...parentId]
          var parentArr = [...parents]
          var childrenid = childrenid
          delete parentArr.children
          parentid.push(item[this.idKey])
          parentArr.push({
            [this.idKey]: item[this.idKey],
            [this.rangeKey]: item[this.rangeKey]
          })
          this.renderTreeList(item.children, rank + 1, parentid, parentArr)
        } else {
          this.treeList[this.treeList.length - 1].lastRank = true
        }
      })
    },
    initTree(range = this.list) {
      this.treeList = []
      this.renderTreeList(range)
    }
  }
}
</script>

<style scoped>
.tki-tree-item {
  height: 0;
  opacity: 0;
  transition: 0.2s;
}

.tki-tree-item.show {
  height: 80rpx;
  opacity: 1;
}

.tki-tree-item.showchild:before {
  transform: rotate(90deg);
}

.tki-tree-item.last:before {
  opacity: 0;
}

.tki-tree-icon {
  width: 26rpx;
  height: 26rpx;
  margin-right: 8px;
}

.tki-tree-label {
  flex: 1;
  display: flex;
  align-items: center;
}
</style>
